<?php
class Product_Model_OrderPickupMap
{

	private $_table;
	//Initalizing Db Table  
	public function __construct(){
		$this->_table = new Product_Model_DbTable_OrderPickupMap();
	}
	
	public  function create($row){
		return $this->_table->insert($row);
	}

	public  function update($row,$value,$field='orders_code'){
		$where = $this->_table->getAdapter()->quoteInto("{$field} = ?",$value);
		return $this->_table->update($row, $where);

	}
	public  function getByCondition($condition=array(),$type='*',$page="",$pageSize=""){
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table,$type);
        $select->where("1 =?",1);
		if(isset($condition['op_code']) && !empty($condition['op_code'])){
			$select->where("op_code = ?",$condition['op_code']);
		}
		if(isset($condition['orders_code']) && !empty($condition['orders_code'])){
			$select->where("orders_code = ?",$condition['orders_code']);
		}
        if('count(*)'==$type){
            return $this->_table->getAdapter()->fetchOne($select);
        }else{
            if($pageSize>0 and $page>0)
            {
                $start =($page-1)*$pageSize;
                $select->limit($pageSize,$start);
            }
			$sql=$select->__toString();
		    return $this->_table->getAdapter()->fetchAll($sql);
        }
	}
	
	public function getByValue($value,$field){
        $select = $this->_table->getAdapter()->select();
        $table  = $this->_table->info('name');
        $select->from($table,'*');
        $select->where("{$field} = ?",$value);
		return $this->_table->getAdapter()->fetchRow($select);
	}
	
	public function groupByProductId($ordersCode){
		$select = $this->_table->getAdapter()->select();
		$table  = $this->_table->info('name');
		$select->from($table,' orders_code,product_id,sum(opm_quantity) opm_quantity');
		$select->where("orders_code = ?",$ordersCode);
		$select->group("product_id");
// 		echo $select->__toString();
		return $this->_table->getAdapter()->fetchAll($select);
	}
	
	
	
	public function groupByWsCode($condition=array(),$orderby=array(),$count=0, $offset=0){
		$select = $this->_table->getAdapter()->select();
		$table = $this->_table->info('name');
		$select->from($table,"ws_code,count(opm_quantity) count,orders_code,product_id");
		if(isset($condition["op_code"]) && $condition["op_code"]!=''){
			$select->where(" op_code = ?",$condition["op_code"]);
		}
	
		$select->group("ws_code");
		$select->order($orderby);
		$select->limit($count,$offset);
		$sql=$select->__toString();
		//print_r($sql);exit;
		$result =  $this->_table->getAdapter()->fetchAll($sql);
	
		return $result;
	
	}
	
	
	public  function updateByWscode($row,$opCode=0,$wsCodeArr=array(0))
	{
		$where = $this->_table->getAdapter()->quoteInto('op_code = ?',$opCode );
		$where .= $this->_table->getAdapter()->quoteInto(' and ws_code in(?)',$wsCodeArr);
		return $this->_table->update($row, $where);
	}
	
	public function getOrdersByWsCode($opCode=0,$wsCodeArr,$orderby=array("ws_code asc")){
		$select = $this->_table->getAdapter()->select();
		$table = $this->_table->info('name');
		$select->from($table,"orders_code");
		
		$select->where(" op_code = ?",$opCode);
		$select->where(" ws_code in(?)",$wsCodeArr);

		$select->group("ws_code");
		$select->order($orderby);
		$sql=$select->__toString();
		//echo $sql."<br>";
		//print_r($wsCodeArr);exit;
		$result =  $this->_table->getAdapter()->fetchAll($sql);
	
		return $result;
	
	}
	
}